package com.ph.website.home.service.impl;

import com.ph.website.home.base.common.CommonResult;
import com.ph.website.home.base.common.DataGrid;
import com.ph.website.home.base.common.Ref;
import com.ph.website.home.base.utils.TimeUtil;
import com.ph.website.home.entity.HelpSupport;
import com.ph.website.home.mapper.HelpSupportMapper;
import com.ph.website.home.param.HelpSupportParam;
import com.ph.website.home.service.IHelpSupportService;
import com.ph.website.home.vo.HelpSupportVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class HelpSupportServiceImpl implements IHelpSupportService {

    @Resource
    private HelpSupportMapper helpSupportMapper;

    /**后台查询列表*/
    public CommonResult list(HelpSupportParam.ListParam param){

        List<HelpSupport> list = helpSupportMapper.list(param.getStart(), param.getNum(),
                param.getHelpType(), param.getHelpState(), param.getKey());

        int count = helpSupportMapper.listCount(param.getHelpType(), param.getHelpState(), param.getKey());

        List<HelpSupportVo.ListVo> vos = new ArrayList<>();
        for(HelpSupport help : list){
            HelpSupportVo.ListVo vo = new HelpSupportVo.ListVo();
            vo.setHelpId(help.getId());
            vo.setQuestion(help.getQuestion());
            vo.setAnswer(help.getAnswer());
            vo.setHelpState(help.getHelpState());
            vo.setHelpType(help.getHelpType());
            vo.setOrderNum(help.getOrderNum());
            vo.setCreateTime(TimeUtil.formaterYYYYMMddHHmm(help.getCreateTime()));
            vos.add(vo);
        }

        DataGrid<HelpSupportVo.ListVo> dataGrid = new DataGrid<>();
        dataGrid.setRows(vos);
        dataGrid.setTotal(count);
        return Ref.ok(dataGrid);
    }

    /**添加*/
    public CommonResult add(HelpSupportParam.AddParam param){
        HelpSupport help = new HelpSupport();
        help.setQuestion(param.getQuestion());
        help.setAnswer(param.getAnswer());
        help.setHelpType(param.getHelpType());
        help.setOrderNum(param.getOrderNum());
        help.setCreateTime(new Date());
        help.setHelpState(0);
        helpSupportMapper.insert(help);
        return Ref.ok();
    }

    /**修改*/
    public CommonResult modify(HelpSupportParam.ModifyParam param){
        HelpSupport help = helpSupportMapper.selectById(param.getHelpId());
        if(help==null){
            return Ref.error("错误的帮助支持编号");
        }
        if(help.getHelpState()==1){
            return Ref.error("已发布的帮助支持不能修改");
        }
        help.setQuestion(param.getQuestion());
        help.setAnswer(param.getAnswer());
        help.setHelpType(param.getHelpType());
        help.setOrderNum(param.getOrderNum());
        helpSupportMapper.updateById(help);
        return Ref.ok();
    }

    /**删除*/
    public CommonResult delete(HelpSupportParam.DeleteParam param){
        HelpSupport help = helpSupportMapper.selectById(param.getHelpId());
        if(help==null){
            return Ref.error("错误的帮助支持编号");
        }
        if(help.getHelpState()==1){
            return Ref.error("已发布的帮助支持不能删除");
        }
        helpSupportMapper.deleteById(param.getHelpId());
        return Ref.ok();
    }

    /**发布*/
    public CommonResult publish(HelpSupportParam.PublishParam param){
        HelpSupport help = helpSupportMapper.selectById(param.getHelpId());
        if(help==null){
            return Ref.error("错误的帮助支持编号");
        }
        help.setHelpState(1);
        helpSupportMapper.updateById(help);
        return Ref.ok();
    }

    /**取消发布*/
    public CommonResult unPublish(HelpSupportParam.UnPublishParam param){
        HelpSupport help = helpSupportMapper.selectById(param.getHelpId());
        if(help==null){
            return Ref.error("错误的帮助支持编号");
        }
        help.setHelpState(0);
        helpSupportMapper.updateById(help);
        return Ref.ok();
    }
}
